package com.example.meitao.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.example.meitao.R;
import com.example.meitao.ui.SearchActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private Fragment mCurrentFragment;
    private FragmentManager mManager;
    private RadioGroup mRadioGroup;


    private ImageView iv_home_search,iv_home_all;
    private PopupWindow mPopupWindow;
   // private boolean flag =false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        initData();
        setListener();
        return view;

    }

    private void setListener() {
        iv_home_all.setOnClickListener(this);
        iv_home_search.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Class<? extends Fragment> clazz = null;
                switch (i){
                    case R.id.rb_home_discount:
                        clazz = DiscountFragment.class;
                        break;
                    case R.id.rb_home_community:
                        clazz = CommunityFragment.class;
                        break;
                }
                showAndHide(R.id.fl_home_fragment,clazz);
            }
        });
    }

    private void initView(View view) {
        mRadioGroup = (RadioGroup) view.findViewById(R.id.rg_homg_show);
        //初始化全部和搜索
        iv_home_all = (ImageView) view.findViewById(R.id.iv_home_all);
        iv_home_search = (ImageView) view.findViewById(R.id.iv_home_search);
    }

    private void initData() {
        mManager = getActivity().getSupportFragmentManager();
        showAndHide(R.id.fl_home_fragment,DiscountFragment.class);
    }
    private void showAndHide(int contentId, Class<? extends Fragment> clazz){
        if(mCurrentFragment!=null&&mCurrentFragment.getClass().getSimpleName().equals(clazz.getCanonicalName())) {
            return;
        }
        FragmentTransaction transaction = mManager.beginTransaction();
        Fragment fragment = null;
        try {
            //判断fragment有没有添加过
            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
            if(fragmentByTag!=null) {
                //显示需要 显示的fragment
                transaction.show(fragmentByTag);
                //隐藏当前的fragment
                transaction.hide(mCurrentFragment);
                //记录当前显示的fragment
                mCurrentFragment = fragmentByTag;
            }else{
                //通过无参的 公开的 构造函数来创建实例
                fragment = clazz.newInstance();
                //当前的fragment没有添加过
                //吧fragment添加到fragmentManager里
                transaction.add(contentId,fragment,clazz.getSimpleName());
                //隐藏当前的fragment
                if(mCurrentFragment!=null) {
                    transaction.hide(mCurrentFragment);
                }
                //记录当前的fragment是哪个
                mCurrentFragment = fragment;
            }
            transaction.commit();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

//        transaction.show(fragment);
//        transaction.hide(fragment);
//        transaction.add(fragment,fragment.getClass().getSimpleName());
//        mManager.findFragmentByTag(fragment.getClass().getSimpleName());
    }

    private void initPopwidndow(){
        //TODO 获取popwidow
        View conrentview =LayoutInflater.from(getActivity()).inflate(R.layout.item_home_pop,null);
        mPopupWindow =new PopupWindow(conrentview,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
        mPopupWindow.setContentView(conrentview);

        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
       // mPopupWindow.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        mPopupWindow.setBackgroundDrawable(dw);
        //设置popwidow
        mPopupWindow.showAsDropDown(iv_home_all);

        //popWindow消失监听方法
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                mPopupWindow.dismiss();
            }
        });

    }


    @Override
    public void onClick(View view) {
        if(view!=null) {
            switch (view.getId()) {
                case R.id.iv_home_search:
                    startActivity(new Intent(getActivity(), SearchActivity.class));

                    break;
                case R.id.iv_home_all:

                    initPopwidndow();
                   /* if(flag){
                        mPopupWindow.dismiss();
                        flag =false;
                    }else{
                        initPopwidndow();
                        flag =true;
                    }*/
                    break;

            }
        }
    }
}
